($ => {
    // <summary>
    // 获取注册页面的工具栏配置
    // params: json自定义参数
    // </summary>
    $.getRegisterTools = ($eventEmitter, quickFilter, advancedSearch, examGrid, form, toolbar, params) => {
        const handleClick = (toolId, data) => {
            $eventEmitter.trigger(toolId, {
                quickFilter,
                advancedSearch,
                examGrid,
                form,
                toolbar,
                updateToolbarItems,
                params,
                data
            });
        };
        const tool = {
            // 新建
            new: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_新建',
                options: {
                    text: '新建',
                    icon: 'add',
                    focusStateEnabled: false,
                    onClick: (e) => {
                        if (params) {
                            let { deviceType } = params;
                            if (deviceType instanceof Array) {
                                deviceType = deviceType[0];
                            }
                            handleClick('new', deviceType);
                        }
                    },
                },
            },
            // 下拉框新建
            deviceTypeSelect: {
                location: 'before',
                widget: 'dxDropDownButton',
                name: '登记_选择新建',
                options: {
                    focusStateEnabled: false,
                    // stylingMode: 'outlined',
                    text: '新建',
                    icon: 'add',
                    displayExpr: 'name',
                    keyExpr: 'id',
                    onItemClick(e) {
                        handleClick('new', e.itemData);
                    },
                },
            },
            // 打开
            open: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_打开',
                options: {
                    text: '打开',
                    icon: 'folder',
                    focusStateEnabled: false,
                    onClick: ({ itemData }) => {
                        handleClick('open', (itemData && itemData.key));
                    },
                },
            },
            // 保存
            save: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_保存',
                options: {
                    text: '保存',
                    icon: 'save',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('save');
                    },
                },
            },
            // 编辑
            edit: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_编辑',
                options: {
                    text: '编辑',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('edit');
                    },
                },
            },
            // 删除
            delete: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_删除',
                options: {
                    text: '删除',
                    icon: 'trash',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('delete');
                    },
                },
            },
            // 刷新
            refresh: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '刷新',
                    icon: 'refresh',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('refresh');
                    },
                },
            },
            // 刷新设置
            refreshSetting: {
                widget: 'dxDropDownButton',
                location: 'after',
                options: {
                    text: '刷新设置',
                    icon: 'preferences',
                    focusStateEnabled: false,
                    dropDownOptions: {
                        minWidth: '160px',
                    }
                },
            },
            // 自动刷新
            autoRefresh: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '启用自动刷新',
                    icon: 'refresh',
                    focusStateEnabled: false,
                    onClick: (e) => {
                        handleClick('autoRefresh', { component: e, key: 'registerRefreshInterval', intervalKey: 'registerInterval' });
                    },
                },
            },
            // 刷新间隔时间
            refreshInterval: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '设置间隔时间',
                    icon: 'clock',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('refreshInterval', { key: 'registerRefreshInterval', intervalKey: 'registerInterval' });
                    },
                },
            },
            // 提交
            submit: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_提交',
                options: {
                    text: '提交',
                    icon: '/icons/fa_check.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('submit');
                    },
                },
            },
            // 撤回
            revoke: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_撤回',
                options: {
                    text: '撤回',
                    icon: 'arrowback',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('revoke');
                    },
                },
            },
            // 退费
            refund: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_退费',
                options: {
                    text: '退费申请',
                    icon: '/icons/md_attach_money.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('refund');
                    },
                },
            },
            // 查看报告
            viewReport: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_查看报告',
                options: {
                    text: '查看报告',
                    icon: '/icons/antd_file_pdf.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('viewReport');
                    },
                },
            },
            // 查看图像
            viewImage: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_查看图像',
                options: {
                    text: '查看图像',
                    icon: '/icons/antd_image.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('viewImage');
                    },
                },
            },
            // 打印
            print: {
                widget: 'dxDropDownButton',
                location: 'before',
                name: '登记_打印',
                options: {
                    text: '打印',
                    icon: 'print',
                    focusStateEnabled: false,
                    displayExpr: 'text',
                    keyExpr: 'id',
                    dropDownOptions: {
                        minWidth: '170px',
                    }
                },
            },
            // 扫描单据
            scan: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_扫描单据',
                options: {
                    text: '扫描单据',
                    icon: '/icons/antd_scan.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('scan');
                    },
                },
            },
            // 数据来源
            dataSourceSelect: {
                location: 'before',
                widget: 'dxSelectBox',
                name: '登记_数据来源',
                options: {
                    width: '100',
                    text: '选择数据源...',
                    focusStateEnabled: false,
                    useSelectMode: true,
                    displayExpr: 'name',
                    valueExpr: 'id',
                    stylingMode: 'Underlined',
                    //onSelectionChanged(e) {
                    //    handleClick('dataSourceSelect', e.item);
                    //}
                    onValueChanged(e) {
                        handleClick('dataSourceValueChanged', e);
                    }
                },
            },
            // 读取身份证
            idCard: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_读取身份证',
                options: {
                    text: '读取身份证',
                    icon: '/icons/antd_creditcard.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('swipe', { swipeType: 'IDCard' });
                    },
                },
            },
            // 读取医保卡
            medicareCard: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_读取医保卡',
                options: {
                    text: '读取医保卡',
                    icon: '/icons/antd_creditcard.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('swipe', { swipeType: 'MedicareCard' });
                    },
                },
            },
            // 读取就诊卡
            medicalCard: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_读取就诊卡',
                options: {
                    text: '读取就诊卡',
                    icon: '/icons/antd_creditcard.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('swipe', { swipeType: 'MedicalCard' });
                    },
                },
            },
            // 扫码
            scanCode: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_扫码',
                options: {
                    text: '条码扫描',
                    icon: '/icons/antd_scan.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('swipe', { swipeType: 'ScanCode' });
                    },
                },
            },
            // 文本框: 按回车之后，直接弹出检查列表
            search: {
                widget: 'dxTextBox',
                location: 'before',
                name: '登记_编号查询',
                options: {
                    elementAttr: {
                        id: 'searchTextBox'
                    },
                    width: '300px',
                    placeholder: '输入编号，按回车键查询...',
                    showClearButton: true,
                    onEnterKey: (e) => {
                        // 获取文本框的值
                        let text = e.component.option('text');
                        handleClick('search', { text });
                    },
                },
            },
            customQuery: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_查询',
                options: {
                    text: '查询',
                    icon: 'search',
                    focusStateEnabled: false,
                    onClick: () => {
                        var searchTextBox = $('#searchTextBox').dxTextBox('instance');
                        let text = searchTextBox.option('text');
                        handleClick('search', { text });
                    },
                },
            },
            // 自动合并
            autoMerge: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    text: '自动合并',
                    icon: 'swipe',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('autoMerge');
                    },
                },
            },
            // 合并
            merge: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    text: '合并',
                    icon: 'swipe',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('merge');
                    },
                },
            },
            // 患者到诊
            arrival: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_患者到诊',
                options: {
                    text: '患者到诊',
                    icon: '/icons/md_outlined_flag.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('arrival');
                    },
                },
            },
            // 其他操作
            others: {
                widget: 'dxDropDownButton',
                location: 'after',
                name: '登记_其他操作',
                options: {
                    text: '其他操作',
                    icon: 'more',
                    focusStateEnabled: false
                },
            },
            // 置顶
            top: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_置顶',
                options: {
                    text: '置到顶部',
                    icon: '/icons/md_vertical_align_top.svg',
                    beginGroup: true,
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('top');
                    },
                },
            },
            // 置前
            previous: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_置前',
                options: {
                    text: '向前一位',
                    icon: '/icons/fa_chevron_up.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('previous');
                    },
                },
                visible: false
            },
            // 置后
            next: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_置后',
                options: {
                    text: '向后一位',
                    icon: '/icons/fa_chevron_down.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('next');
                    },
                },
                visible: false
            },
            // 置底
            bottom: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_置底',
                options: {
                    text: '置到底部',
                    icon: '/icons/md_vertical_align_bottom.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('bottom');
                    },
                },
            },
            // 插入
            insert: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_插入',
                options: {
                    text: '插入',
                    icon: 'arrowright',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('insert');
                    },
                },
            },
            // 挂起
            suspend: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_挂起',
                options: {
                    text: '挂起患者',
                    icon: '/icons/md_pause_circle_outline.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('suspend');
                    },
                },
            },
            // 取消挂起
            cancelSuspend: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_取消挂起',
                options: {
                    text: '取消挂起',
                    icon: '/icons/md_remove_circle_outline.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('cancelSuspend');
                    },
                },
            },
            // 绿色通道
            greenChannel: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_绿色通道',
                options: {
                    text: '绿色通道',
                    icon: '/icons/fa_person_running.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('greenChannel');
                    },
                },
            },
            // 全选
            selectAll: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_全选',
                options: {
                    text: '全选',
                    icon: '/icons/md_select_all.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('selectAll');
                    },
                },
            },
            // 反选
            invertSelect: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_反选',
                options: {
                    text: '反选',
                    icon: '/icons/md_deselect.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('invertSelect');
                    },
                },
            },
            study: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_远程会诊',
                options: {
                    text: '远程会诊',
                    icon: '/icons/x_exam.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('study');
                    },
                },
            },
            consultation: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_申请会诊',
                options: {
                    text: '申请会诊',
                    icon: '/icons/x_exam.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('consultation');
                    },
                },
            },
            waitingQueue: {
                widget: 'dxDropDownButton',
                location: 'before',
                name: '登记_等候队列',
                options: {
                    text: '等候队列',
                    icon: '/icons/x_exam_queuing.svg',
                    focusStateEnabled: false,
                    dropDownOptions: {
                        minWidth: '160px',
                    }
                },
                visible: true
            },
            // 文本框: 按回车之后，直接弹出检查列表
            studySearch: {
                widget: 'dxTextBox',
                location: 'before',
                name: '登记_检查查询',
                options: {
                    width: '300px',
                    placeholder: '输入编号，按回车键查询...',
                    showClearButton: true,
                    onEnterKey: (e) => {
                        // 获取文本框的值
                        let text = e.component.option('text');
                        if (text.trim()) {
                            handleClick('studySearch', { text });
                        }
                    },
                    onValueChanged: ({ value }) => {
                        if (!value) {
                            handleClick('studySearch', { text: value });
                        }
                    }
                },
            },
            // 修正
            edit: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_修正',
                options: {
                    text: '修正',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('edit');
                    },
                },
            },
            // 远程会诊
            // 今天
            today: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '今天',
                    icon: '/icons/ionic_today.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('today');
                    },
                },
            },
            // 昨天
            yesterday: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '昨天',
                    icon: '/icons/ionic_today.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('yesterday');
                    },
                },
            },
            // 近三天
            lastThreeDays: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '近三天',
                    icon: '/icons/ionic_today.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('lastThreeDays');
                    },
                },
            },
            // 申请会诊
            applyConsultation: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_申请会诊',
                options: {
                    text: '申请会诊',
                    icon: '/icons/x_exam.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('applyConsultation');
                    },
                },
            },
            // 同步补录
            supplementarySync: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_同步补录',
                options: {
                    text: '同步补录',
                    icon: '/icons/md_sync.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('supplementarySync');
                    },
                },
            },
            // 查看检查事件
            examEvent: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_检查事件',
                options: {
                    text: '检查事件',
                    icon: '/icons/antd_thunderbolt.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('examEvent');
                    },
                }
            },
            export: {
                widget: 'dxButton',
                location: 'before',
                name: '登记_导出列表',
                options: {
                    text: '导出列表',
                    icon: 'export',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('export');
                    },
                }
            }
        };
        for (const property in tool) {
            const item = tool[property];
            if (item.name) {
                item.visible = item.visible || (ris.verify(item.name) && item.visible !== false);
            }
            if (property === 'waitingQueue') {
                item.visible = tool['suspend'].visible || tool['cancelSuspend'].visible || tool['top'].visible || tool['previous'].visible || tool['next'].visible || tool['bottom'].visible
            }
            if (item.options) {
                if (item.hasOwnProperty('visible')) {
                    item.options.visible = item.visible;
                }
            }
        }
        const updateToolbarItems = (exam, isEdit = false) => {
            const items = toolbar.option('items');
            const permissions = exam.permissions;
            if (items.length > 0) {
                items.forEach(e => {
                    if (["登记_保存", "登记_提交", "登记_撤回", "登记_修正", "登记_患者到诊", "登记_等候队列"].includes(e.name)) {
                        e.disabled = true;
                        e.visible = false;
                    }
                    if (permissions.includes(e.name)) {
                        e.disabled = false;
                        e.visible = ris.verify(e.name);
                    }
                    if (e.name == '等候队列') {
                        e.visible = permissions.includes(e.name);
                    };
                    if (isEdit) {
                        //修正不显示
                        if (e.name === '登记_修正' || e.name === '登记_提交' || e.name === '登记_撤回') {
                            e.disabled = true;
                            e.visible = false;
                        } if (e.name === '登记_保存') {
                            e.disabled = false;
                            e.visible = true;
                        }
                    }
                })
            }
            toolbar.option('items', items);
        }
        return { ...tool, updateToolbarItems };
    };
})(jQuery);
